﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.Articles;
using SouthernLand.Common.Untilities;

namespace SouthernLand.Articles
{
    public partial class ArticleDetail : BasePage
    {
        private int ArticleID = -1;
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ArticleID = Convert.ToInt32(Request.QueryString["ArticleID"]);
            if (ArticleID <= 0)
                Response.Redirect("Default.aspx");

            CateID = Article.GetArticleCateIDByArticleID(ArticleID);
            Title.Text = ArticleCategory.GetCateNameByCateID(CateID);

            if (string.IsNullOrEmpty(Article.GetImageURLByArticleID(ArticleID)))
                TableImage.Visible = false;


            Article obj = Article.GetArticleByArticleID(ArticleID);
            if (obj != null)
            {
                lblPostDate.Text = "(" + Formatter.FormatDateTime(obj.PostDate) + ")";
                lblTitle.Text = obj.Title;
                FrameTitle.Text = obj.Title;
                lblExcerpt.Text = obj.Excerpt;
                lblBody.Text = obj.Body;
                ImageArticle.ImageUrl = ApplicationPath + "UserUpload/Articles/" + obj.ImageURL;
            }

            Article.UpdateTotalViews(ArticleID);
            Load_ThongBaoKhac();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rptArticleOther.ItemDataBound += rptArticleOther_ItemDataBound;
        }

        private void rptArticleOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var article = (Article) e.Item.DataItem;
                if (article != null)
                {
                    var hplAnotherInformations = (HyperLink) e.Item.FindControl("hplAnotherInformations");
                    hplAnotherInformations.Text = article.Title;
                    hplAnotherInformations.NavigateUrl = Globals.BuildUrlByNews(article.ArticleID,
                                                                                Globals.ProcessString(
                                                                                    Globals.UnicodeToKoDau(article.Title)));
                }
            }
        }

        protected void Load_ThongBaoKhac()
        {
            List<Article> lstArticles = Article.GetArticleOtherArticleID(ArticleID, CateID);
            if (lstArticles != null && lstArticles.Count > 0)
            {
                rptArticleOther.DataSource = lstArticles;
                rptArticleOther.DataBind();
            }
        }
    }
}